package com.itheima.tliassystem.service.impl;
import com.itheima.tliassystem.mapper.DepMapper;
import com.itheima.tliassystem.mapper.EmpMapper;
import com.itheima.tliassystem.service.DepService;
import entity.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileNotFoundException;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DepServiceImpl implements DepService {
    @Autowired
    DepMapper depMapper;
    @Autowired
    EmpMapper empMapper;


    @Override
    public List<Dept> listDept() {
        List<Dept> deptList = depMapper.selectAll();
        return deptList;
    }

    @Override
    public void addDept(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        depMapper.insert(dept);
     }
    @Override
    //rollbackFor  非运行异常进行事务管理 可以用这个
    @Transactional(rollbackFor = Exception.class)
    public void remove(Integer id) throws FileNotFoundException {
      //  int i = 1/0;
        depMapper.deleteById(id);
        //编译时错误不会误删
     if (true){
         throw  new FileNotFoundException();
     }
        empMapper.deleteById1(id);
    }

    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        dept.setCreateTime(LocalDateTime.now());
        depMapper.upDate(dept);
    }

    @Override
    public Dept selectById(Integer id) {
       Dept dept = depMapper.selectById( id);
        return dept;
    }

}
